/**
 * Created by tianjie on 2017/11/27.
 */
$(function () {
    doRank();
})

var param;

function doReRank() {
    param  = {"gameTypeId":$("#selectType1").val()};
    param.rankInfo = "negative";
    doQuery(param);
}

function doRank() {
    param = {"gameTypeId":$("#selectType1").val()};
    param.rankInfo = "positive";
    doQuery(param);
}

function doQuery(data) {
    $.ajax({
        url:"/admin/statistics/PlayGameRank?random="+Math.random(),
        type:'POST', //GET
        async:false,    //或false,是否异步
        data: data,
        dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
        success:function(data,textStatus,jqXHR){
            console.log(data);
            if(data.code == 0){
                $("#rank-table").html("");
                var str = "<thead><tr><th>排名</th><th>角色ID</th><th>角色名称</th><th>胜率</th><th>胜利局数</th>" +
                    "<th>总局数</th><th>总得分</th></tr></thead>";
                if(data.data.length > 0){
                    $.each(data.data, function (i, n) {
                        str += "<tr><td>"+(i+1)+"</td><td>"+n.roleUid+"</td><td>"+n.roleName+"</td><td>"
                            +WinRate(n.totalWin,n.totalRound)+"</td><td>" +n.totalWin+ "</td><td>"
                            +n.totalRound+"</td><td>"+n.totalScore +"</td></tr>";
                    });
                }else {
                    str += "<tr><td colspan='6'>没有找到匹配的记录</td></tr>"
                }
                $("#rank-table").append(str);
            }else {
                alert(data.msg);
            }
        },
        error:function(xhr,textStatus){
            console.log('错误')
            console.log(xhr)
            console.log(textStatus)
        }
    })
}

function WinRate(totalWin,totalRound) {
    var num;
    if(totalWin * 100 %totalRound ==0){
        num = (totalWin/totalRound * 100).toFixed(0);
    }else {
        num = (totalWin/totalRound * 100).toFixed(2);
    }
    return num + "%";
}
